//
//  BaseImageV.m
//  ZITHER
//
//  Created by weixin on 2018/10/15.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "BaseImageV.h"

@implementation BaseImageV

-(instancetype)initializeFrame:(CGRect)frame contentMode:(NSInteger)contentMode{
    [self init].frame = frame;
    self.contentMode = contentMode;
    self.backgroundColor = [UIColor randomColor];
    self.clipsToBounds = YES;
    return self;
}

+(instancetype)initCustom:(CGRect)frame contentMode:(NSInteger)contentMode{
    BaseImageV *imag = [[BaseImageV alloc]init];
    imag.frame = frame;
    imag.contentMode = contentMode;
    imag.backgroundColor = [UIColor randomColor];
    imag.clipsToBounds = YES;
    return imag;
}

-(void)sd_setImage:(NSString*)url{
    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,url];
    [self sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
}


-(RACSignal *)addTag{
    /** 添加点击事件 */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
    return [tap rac_gestureSignal];

}


-(void)setRound:(CGFloat)width{
    [self setCornerRadiusWithRadius:width isMasks:YES];
}

//加载国旗
-(void)sd_setImageFlags:(NSString*)url{
    //需要对图标作区分
    
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
}

/** 添加点击事件 */
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//
//[[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//    if (self.showBigPic) {
//        self.showBigPic(self.indexPath,imageV.tag-100);
//    }
//}];
//    self.userInteractionEnabled = YES;
//[imageV addGestureRecognizer:tap];

/** 加载图片 */
//            NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,self.model.arrMPic[i]];
//            [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];


-(void)addBigImage{
    self.tag = 100;
    [[self addTag]subscribeNext:^(id  _Nullable x) {
        [self showBigImage:self.tag];
    }];

}


-(void)showBigImage:(NSInteger)selectIndex{
    NSLog(@"107w行 输出点击selectIndex%ld",selectIndex);
    
    UIImageView *imageV = [self viewWithTag:selectIndex];
    // self.model.fileList[selectIndex-100];
    
    
    if (!self.imagePreviewViewController) {
        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        self.imagePreviewViewController.imagePreviewView.delegate = self;
        
        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
        
    }
    
    __weak __typeof(self)weakSelf = self;
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        NSLog(@"124行 输出aImagePreviewViewController.imagePreviewView.currentImageIndex%ld",aImagePreviewViewController.imagePreviewView.currentImageIndex);
        
        NSInteger index = aImagePreviewViewController.imagePreviewView.currentImageIndex;
        UIImageView *imageV1 = [weakSelf viewWithTag:index+100];
        NSLog(@"124行 输出index+100%ld",index+100);
        
//        imageV.image = currentZoomImageView.image;
//        [weakSelf.imageButton setImage:currentZoomImageView.image forState:UIControlStateNormal];
        
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:[weakSelf convertRect:imageV1.frame toView:nil]];
        
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  0;// 默认查看的图片的 index
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[self.superview convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
}




#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return 1;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    
    
    NSLog(@"154行 输出index+100%ld",index+100);
    
    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];
    if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
        [zoomImageView hideEmptyView];
        
        UIImageView *imageV = [self viewWithTag:index+100];
        zoomImageView.image = imageV.image;
        
    }
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //
    //    });
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    UIGraphicsBeginImageContext(newsize);
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
- (void)setImageChangeGray:(NSString *)Url{
    [self sd_setImageWithURL:[NSURL URLWithString:Url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        self.image = image.grayImage;//图片变灰
    }];
}


@end
